#!/usr/local/bin/python
# -*- coding: utf-8 -*-
from django.test import TestCase
from django.test.client import Client
from os.path import dirname, join

class Test(TestCase):
    fixtures = [join(dirname(__file__), "fixtures/leave.json")]
    def test_home_anonymous(self):
        client = Client()
        response = client.get('/leave/')
        self.failUnlessEqual(response.status_code, 200)

    def test_otherswork_anonymous(self):
        "TODO: should be also be tested with proper authentication!"
        client = Client()
        response = client.get('/leave/otherswork/', {'worker':'primus'})
        self.failUnlessEqual(response.status_code, 302)
        response = client.get('/leave/otherswork/', {'worker':'secundus'})
        self.failUnlessEqual(response.status_code, 302)
        response = client.get('/leave/otherswork/', {'worker':'tertius'})
        self.failUnlessEqual(response.status_code, 302)

    def test_details(self):
        client = Client()
        ok = client.login(username='primus', password='bad_passwd')
        self.assertFalse(ok, 'authentication not ok')
        ok = client.login(username='primus', password='p')
        self.assertTrue(ok, 'authentication ok')

        ok = client.login(username='admin', password='open')
        self.assertTrue(ok, 'authentication ok')
        response = client.get('/leave/admin/')
        self.failUnlessEqual(response.status_code, 200)
        client.logout()

    def test_run(self):
        c = Client()
        r = c.get('/leave/', {})
        self.assertEqual(r.status_code, 200)
        self.assertEqual(unicode(r.context[-1]['params']), '{}')
        r = c.get('/leave/accounts/login/', {'next': 'home/',})
        self.assertEqual(r.status_code, 200)
        self.assertEqual(unicode(r.context[-1]['site_name']), 'example.com')
        self.assertEqual(unicode(r.context[-1]['next']), 'home/')
        r = c.post('/leave/accounts/login/', {'username': 'primus','post_data': '','password': 'p','this_is_the_login_form': '1',})
        r = c.get('/leave/accounts/login/home/', {})
        self.assertEqual(r.status_code, 301)
        r = c.get('/leave/', {})
        self.assertEqual(r.status_code, 200)
        self.assertEqual(unicode(r.context[-1]['params']), '{}')
        r = c.get('/leave/start/', {})
        self.assertEqual(r.status_code, 200)
        r = c.post('/leave/start/', {'action': 'OK','reason': '','day_end': '2008-08-07','type': 'Vacation','day_start': '2008-08-07',})
        r = c.get('/leave/start/home/', {})
        self.assertEqual(r.status_code, 301)
        r = c.get('/leave/', {})
        self.assertEqual(r.status_code, 200)
        self.assertEqual(unicode(r.context[-1]['params']), '{}')
        r = c.get('/leave/logout/', {'next': 'home/',})
        self.assertEqual(r.status_code, 200)
        self.assertEqual(unicode(r.context[-1]['title']), 'Logged out')
        r = c.get('/leave/', {})
        self.assertEqual(r.status_code, 200)
        self.assertEqual(unicode(r.context[-1]['params']), '{}')
        r = c.get('/leave/accounts/login/', {'next': 'home/',})
        self.assertEqual(r.status_code, 200)
        self.assertEqual(unicode(r.context[-1]['site_name']), 'example.com')
        self.assertEqual(unicode(r.context[-1]['next']), 'home/')
        r = c.post('/leave/accounts/login/', {'username': 'notarius','post_data': '','password': 'n','this_is_the_login_form': '1',})
        r = c.get('/leave/accounts/login/home/', {})
        self.assertEqual(r.status_code, 301)
        r = c.get('/leave/', {})
        self.assertEqual(r.status_code, 200)
        self.assertEqual(unicode(r.context[-1]['params']), '{}')
        r = c.get('/leave/mywork/', {})
        self.assertEqual(r.status_code, 200)
        r = c.get('/leave/mywork/activate/1/', {})
        self.assertEqual(r.status_code, 302)
        r = c.get('/leave/checkstatus/1/', {})
        self.assertEqual(r.status_code, 200)
        r = c.post('/leave/checkstatus/1/', {'action': 'OK: Forward to supervisor','workitem_id': '','reason_denial': '',})
        r = c.get('/leave/checkstatus/1/home/', {})
        self.assertEqual(r.status_code, 301)
        r = c.get('/leave/', {})
        self.assertEqual(r.status_code, 200)
        self.assertEqual(unicode(r.context[-1]['params']), '{}')
        r = c.get('/leave/logout/', {'next': 'home/',})
        self.assertEqual(r.status_code, 200)
        self.assertEqual(unicode(r.context[-1]['title']), 'Logged out')
        r = c.get('/leave/', {})
        self.assertEqual(r.status_code, 200)
        self.assertEqual(unicode(r.context[-1]['params']), '{}')
        r = c.get('/leave/accounts/login/', {'next': 'home/',})
        self.assertEqual(r.status_code, 200)
        self.assertEqual(unicode(r.context[-1]['site_name']), 'example.com')
        self.assertEqual(unicode(r.context[-1]['next']), 'home/')
        r = c.post('/leave/accounts/login/', {'username': 'prefectus','post_data': '','password': 'p','this_is_the_login_form': '1',})
        r = c.get('/leave/accounts/login/home/', {})
        self.assertEqual(r.status_code, 301)
        r = c.get('/leave/', {})
        self.assertEqual(r.status_code, 200)
        self.assertEqual(unicode(r.context[-1]['params']), '{}')
        r = c.get('/leave/mywork/', {})
        self.assertEqual(r.status_code, 200)
        r = c.get('/leave/mywork/activate/2/', {})
        self.assertEqual(r.status_code, 302)
        r = c.get('/leave/approvalform/2/', {})
        self.assertEqual(r.status_code, 200)
        r = c.post('/leave/approvalform/2/', {'action': 'OK: Forward to secretary','workitem_id': '','reason_denial': '',})
        r = c.get('/leave/approvalform/2/home/', {})
        self.assertEqual(r.status_code, 301)
        r = c.get('/leave/', {})
        self.assertEqual(r.status_code, 200)
        self.assertEqual(unicode(r.context[-1]['params']), '{}')
        r = c.get('/leave/logout/', {'next': 'home/',})
        self.assertEqual(r.status_code, 200)
        self.assertEqual(unicode(r.context[-1]['title']), 'Logged out')
        r = c.get('/leave/', {})
        self.assertEqual(r.status_code, 200)
        self.assertEqual(unicode(r.context[-1]['params']), '{}')
        r = c.get('/leave/accounts/login/', {'next': 'home/',})
        self.assertEqual(r.status_code, 200)
        self.assertEqual(unicode(r.context[-1]['site_name']), 'example.com')
        self.assertEqual(unicode(r.context[-1]['next']), 'home/')
        r = c.post('/leave/accounts/login/', {'username': 'notarius','post_data': '','password': 'n','this_is_the_login_form': '1',})
        r = c.get('/leave/accounts/login/home/', {})
        self.assertEqual(r.status_code, 301)
        r = c.get('/leave/', {})
        self.assertEqual(r.status_code, 200)
        self.assertEqual(unicode(r.context[-1]['params']), '{}')
        r = c.get('/leave/mywork/', {})
        self.assertEqual(r.status_code, 200)
        r = c.get('/leave/mywork/activate/3/', {})
        self.assertEqual(r.status_code, 302)
        r = c.get('/leave/hrform/3/', {})
        self.assertEqual(r.status_code, 200)
        r = c.post('/leave/hrform/3/', {'action': 'OK',})
        r = c.get('/leave/hrform/3/home/', {})
        self.assertEqual(r.status_code, 301)
        r = c.get('/leave/', {})
        self.assertEqual(r.status_code, 200)
        self.assertEqual(unicode(r.context[-1]['params']), '{}')
        r = c.get('/leave/logout/', {'next': 'home/',})
        self.assertEqual(r.status_code, 200)
        self.assertEqual(unicode(r.context[-1]['title']), 'Logged out')
        r = c.get('/leave/', {})
        self.assertEqual(r.status_code, 200)
        self.assertEqual(unicode(r.context[-1]['params']), '{}')
        r = c.get('/leave/accounts/login/', {'next': 'home/',})
        self.assertEqual(r.status_code, 200)
        self.assertEqual(unicode(r.context[-1]['site_name']), 'example.com')
        self.assertEqual(unicode(r.context[-1]['next']), 'home/')
        r = c.post('/leave/accounts/login/', {'username': 'primus','post_data': '','password': 'p','this_is_the_login_form': '1',})
        r = c.get('/leave/accounts/login/home/', {})
        self.assertEqual(r.status_code, 301)
        r = c.get('/leave/', {})
        self.assertEqual(r.status_code, 200)
        self.assertEqual(unicode(r.context[-1]['params']), '{}')
        r = c.get('/leave/mywork/', {})
        self.assertEqual(r.status_code, 200)
        r = c.get('/leave/mywork/activate/4/', {})
        self.assertEqual(r.status_code, 302)
        r = c.get('/leave/finalinfo/4/', {})
        self.assertEqual(r.status_code, 200)
        r = c.post('/leave/finalinfo/4/', {'action': 'OK',})
        r = c.get('/leave/finalinfo/4/home/', {})
        self.assertEqual(r.status_code, 301)
        r = c.get('/leave/', {})
        self.assertEqual(r.status_code, 200)
        self.assertEqual(unicode(r.context[-1]['params']), '{}')
        r = c.get('/leave/logout/', {'next': 'home/',})
        self.assertEqual(r.status_code, 200)
        self.assertEqual(unicode(r.context[-1]['title']), 'Logged out')




